package nio.example.study.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.handler.flush.FlushConsolidationHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.traffic.GlobalTrafficShapingHandler;
import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.UnorderedThreadPoolEventExecutor;
import nio.example.study.server.codec.OrderFrameDecoder;
import nio.example.study.server.codec.OrderFrameEncoder;
import nio.example.study.server.codec.OrderProtocolDecoder;
import nio.example.study.server.codec.OrderProtocolEncoder;
import nio.example.study.server.handler.MetricHandler;
import nio.example.study.server.handler.OrderServerProcessHandler;


import java.util.concurrent.ExecutionException;

public class Server1 {

/*	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ServerBootstrap serverBootstrap = new ServerBootstrap();
		serverBootstrap.channel(EpollServerSocketChannel.class);
		serverBootstrap.handler(new LoggingHandler(LogLevel.INFO));
		EpollEventLoopGroup boss = new EpollEventLoopGroup(0, new DefaultThreadFactory("boss"));
		EpollEventLoopGroup worker = new EpollEventLoopGroup(0, new DefaultThreadFactory("worker"));
		serverBootstrap.group(boss, worker);

		MetricHandler metricHandler = new MetricHandler();
		UnorderedThreadPoolEventExecutor eventExecutors = new UnorderedThreadPoolEventExecutor(10, new DefaultThreadFactory("bussiness"));
		// EpollEventLoopGroup bussiness = new EpollEventLoopGroup(0, new DefaultThreadFactory("bussiness"));
		GlobalTrafficShapingHandler globalTrafficShapingHandler = new GlobalTrafficShapingHandler(new EpollEventLoopGroup(), 100 * 1024 * 1024,
				100 * 1024 * 1024);

		serverBootstrap.childHandler(new ChannelInitializer<EpollSocketChannel>() {
			@Override
			protected void initChannel(EpollSocketChannel ch) throws Exception {
				ChannelPipeline pipeline = ch.pipeline();
				pipeline.addLast("LoggingHandler", new LoggingHandler(LogLevel.DEBUG));
				// 流量整形
				pipeline.addLast("TsHandler", globalTrafficShapingHandler);

				pipeline.addLast("frameDecoder", new OrderFrameDecoder());
				pipeline.addLast("FrameEncoder", new OrderFrameEncoder());
				pipeline.addLast("ProtocolEncoder", new OrderProtocolEncoder());
				pipeline.addLast("ProtocolDecoder", new OrderProtocolDecoder());
				pipeline.addLast("MetricHandler", metricHandler);
				pipeline.addLast(new LoggingHandler(LogLevel.INFO));

				pipeline.addLast("flushEnhance", new FlushConsolidationHandler(5, true));
				pipeline.addLast(eventExecutors, new OrderServerProcessHandler());
			}
		});
		ChannelFuture channelFuture = serverBootstrap.bind(8090).sync();
		channelFuture.channel().closeFuture().get();
	}*/
}
